﻿using System;
using OpenQA.Selenium;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace CloudStorageLight.Web.Tests
{
    [TestClass]
    public class GeneralScenarioTest
    {
        static IWebDriver driver;
        static string account;


        // クラス内で最初のテストを実行する前に、ClassInitialize を使用してコードを実行してください
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            account = "test001";
            driver = SubScenarioUtil.CreateDriverAndLogin(account);
        }
        //
        // クラス内のテストをすべて実行したら、ClassCleanup を使用してコードを実行してください
        [ClassCleanup()]
        public static void MyClassCleanup() 
        {
            driver.Close();        
        }

        [TestInitialize]
        public void TestSetUp()
        {
            SubScenarioUtil.Navigate(driver, "f/", account);
        }

        [TestCleanup]
        public void TestCleanup()
        {
        }

        [TestMethod]
        public void 単純なダウンロード()
        {

            SubScenarioUtil.ClearDownloadFile("TEST.txt");
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile("TEST.txt"));
        }

        [TestMethod]
        public void ZIPダウンロード()
        {

            SubScenarioUtil.ClearDownloadFile("downloadfiles.zip");
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "重要.xlsx").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            driver.FindElement(By.Id("download-command")).Click();
            driver.FindElement(By.Id("download-zip-command")).Click();

            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile("downloadfiles.zip"));
        }

        [TestMethod]
        public void 削除したファイルの復元()
        {
            var filePath = "UnitTest.txt";
            File.WriteAllText(filePath, "TEST");
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();

            SubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));
            SubScenarioUtil.Navigate(driver, "f/Demo/", account);
            SubScenarioUtil.GetFolderOrFileElement(driver, filePath);
            SubScenarioUtil.RemoveFile(driver, filePath);

            SubScenarioUtil.Navigate(driver, "f/Demo/", account);
            var deleted = SubScenarioUtil.Exists(driver, filePath);
            Assert.IsTrue(!deleted);

            SubScenarioUtil.ClickMenuCommand(driver, "list-deleted-command");
            SubScenarioUtil.GetFolderOrFileElement(driver, "UnitTest.txt").Click();
            SubScenarioUtil.ClickMenuCommand(driver, "recover-command");

            SubScenarioUtil.Navigate(driver, "f/Demo/", account);

            var recoveried = SubScenarioUtil.GetFolderOrFileElement(driver, filePath);
            Assert.IsTrue(recoveried != null);
        }

        [TestMethod]
        public void 履歴表示()
        {
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            driver.FindElement(By.Id("history-context-command")).Click();
            SubScenarioUtil.TryFind(driver, By.ClassName("icon-history"));
            Assert.IsTrue(driver.FindElements(By.ClassName("icon-history")).Count > 0);
        }

        [TestMethod]
        public void アクセスログ()
        {
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "TEST.txt").Click();
            driver.FindElement(By.Id("accesslog-command")).Click();
            var table = SubScenarioUtil.TryFind(driver, By.ClassName("accesslog"), 120);
            Assert.IsNotNull(table);
        }


        [TestMethod]
        public void 単純な検索()
        {
            //WAIT FOR BUSY
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo");

            SubScenarioUtil.TryFind(driver, By.Id("query")).SendKeys("UnitTest");
            SubScenarioUtil.TryFind(driver, By.Id("search")).Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "UnitTest.txt").Click();
            SubScenarioUtil.ClearDownloadFile("UnitTest.txt");
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile("UnitTest.txt"));
        }

        [TestMethod]
        public void 単純なアップロード()
        {
            var filePath = "UnitTest.txt";
            File.WriteAllText(filePath, "TEST");
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();

            SubScenarioUtil.DeleteFile(driver, filePath);
            SubScenarioUtil.UploadFile(driver, Path.GetFullPath(filePath));

            SubScenarioUtil.Navigate(driver, "f/Demo/", account);
            var uploaded = SubScenarioUtil.GetFolderOrFileElement(driver, filePath);
            Assert.IsTrue(uploaded != null);
        }


        [TestMethod]
        public void パスワードテキストのダウンロードとアップロード()
        {
            var filename = "a.txt";
            var filename2 = "a.txt.zip";
            SubScenarioUtil.ClearDownloadFile(filename2);
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "Password").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, filename).Click();
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile(filename2));
        }


        [TestMethod]
        public void パスワードOffice文書のダウンロードとアップロード()
        {
            var filename = "test.pptx";
            SubScenarioUtil.ClearDownloadFile(filename);
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "Password").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, filename).Click();
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile(filename));
            SubScenarioUtil.RemoveFile(driver, filename);
            SubScenarioUtil.WaitBusy(driver);
            SubScenarioUtil.UploadFile(driver, Path.GetFullPath(filename));
            SubScenarioUtil.WaitBusy(driver);
            SubScenarioUtil.Navigate(driver, "f/Demo/Password/", account);
            var uploaded = SubScenarioUtil.GetFolderOrFileElement(driver, filename);
        }

        [TestMethod]
        public void RMSテキストのダウンロードとアップロード()
        {
            var filename = "a.txt";
            var filename2 = "a.ptxt";
            SubScenarioUtil.ClearDownloadFile(filename2);
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "RMS").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, filename).Click();
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile(filename2));
        }

        [TestMethod]
        public void RMSOffice文書のダウンロードとアップロード()
        {
            var filename = "TEST.pptx";
            var filename2 = "TEST.pptx";
            SubScenarioUtil.ClearDownloadFile(filename2);
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "RMS").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, filename).Click();
            driver.FindElement(By.Id("download-command")).Click();
            Assert.IsTrue(SubScenarioUtil.CheckDownloadFile(filename2));
            SubScenarioUtil.RemoveFile(driver, filename);
            SubScenarioUtil.WaitBusy(driver);
            SubScenarioUtil.UploadFile(driver, Path.GetFullPath(filename2));
            SubScenarioUtil.WaitBusy(driver);
            SubScenarioUtil.Navigate(driver, "f/Demo/RMS/", account);
            var uploaded = SubScenarioUtil.GetFolderOrFileElement(driver, filename);
        }

        [TestMethod]
        public void フォルダ作成()
        {
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            if (SubScenarioUtil.Exists(driver, "単体テストFolder"))
            {
                SubScenarioUtil.DeleteFolder(driver, "単体テストFolder");
            }
            SubScenarioUtil.ClickMenuCommand(driver, "newfolder-command");
            driver.FindElement(By.Id("newFolderName")).SendKeys("単体テストFolder");
            driver.FindElement(By.Id("create-newFolder")).Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "単体テストFolder").Click();
        }

        [TestMethod]
        public void フォルダ設定()
        {
            SubScenarioUtil.GetFolderOrFileElement(driver, "Demo").Click();
            SubScenarioUtil.GetFolderOrFileElement(driver, "Sub").Click();
            SubScenarioUtil.ClickMenuCommand(driver, "folder-setup-command");
            var ele = SubScenarioUtil.TryFind(driver, By.Id("AccessTokens_0__Role"), 10);
            var ch = driver.FindElements(By.Id("AccessTokens_0__InUse"));
            if (ch.Count > 0)
            {
                ch[0].Click();
                SubScenarioUtil.TryClick(driver, By.Id("blobItemDetailOkButton"));
            }
            SubScenarioUtil.ClickMenuCommand(driver, "folder-setup-command");
            ele = SubScenarioUtil.TryFind(driver, By.Id("AccessTokens_0__Role"), 10);
            ele.SendKeys("sales");
            ele = SubScenarioUtil.TryFind(driver, By.Id("AccessTokens_0__CanRead"));
            ele.Click();
            SubScenarioUtil.TryClick(driver, By.Id("blobItemDetailOkButton"));
            var subFolder = SubScenarioUtil.GetFolderOrFileElement(driver, "Sub10");
            Assert.IsTrue(subFolder != null);
        }

    }
}
